package com.lbn.monitor.widget.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李炳南
 * @date 2017/3/7
 */
public class MonitorView extends RelativeLayout {

    private List<Element> data;

    private final int DATA_VALUE_MAX = 4;

    private final int LINES_Y_MAX = 10;

    private Paint mPaint,alertPaint;

    private TextPaint mTextPaint;

    private int viewHeight;

    private int viewWidth;

    private int textSize;

    private int StrokeWidth;

    private ChartContent chart;

    private int pointColor = Color.parseColor("#313335");

    private int alertColor = Color.parseColor("#B22222");

    private int itemHeight, itemWidth;

    private double maxValue = 1000;

    private double alertLines = 0;

    private String title = "";

    public MonitorView(Context context) {
        this(context, null);
    }


    public MonitorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public MonitorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewHeight = MeasureSpec.getSize(heightMeasureSpec);
        viewWidth = MeasureSpec.getSize(widthMeasureSpec);
        textSize = viewWidth / 40;
        viewHeight = viewHeight - textSize * 8;
        viewWidth = viewWidth - textSize * 7;
        itemHeight = viewHeight / LINES_Y_MAX;
        itemWidth = viewWidth / (DATA_VALUE_MAX - 1);
        setPadding(textSize * 7, 0, 0, 0);
        initPaint();
        initTextPaint();

        chart.setTestSize(textSize);
        chart.setPaint(mPaint);
        chart.setItemSize(itemWidth, itemHeight);
        chart.setTextPaint(mTextPaint);
    }

    private void init() {
        data = new ArrayList<>();
        chart = new ChartContent(getContext());
        addView(chart);
    }


    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(StrokeWidth);
        mPaint.setColor(pointColor);
        mPaint.setStyle(Paint.Style.FILL);
        alertPaint = new Paint(mPaint);
        alertPaint.setColor(alertColor);
        alertPaint.setTextSize(textSize*1.2f);
    }

    /**
     * 初始化文字画笔
     */
    private void initTextPaint() {
        mTextPaint = new TextPaint();
        mTextPaint.setTextSize(textSize);
        mTextPaint.setStrokeWidth(StrokeWidth);
        mTextPaint.setColor(pointColor);
        mTextPaint.setStyle(Paint.Style.FILL);
    }




    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        mTextPaint.setTextSize(textSize * 1.5f);
//        float titleMeasure = mTextPaint.measureText(title);
        canvas.drawText(title, textSize, textSize * 2, mTextPaint);
        canvas.translate(textSize * 7, textSize * 3);
        drawLeftText(canvas);
        drawAlertLines(canvas);
    }

    public void addElement(Element element) {
        chart.addElement(element);
    }

//    String leftText = "xxxxx";

    public void drawLeftText(Canvas canvas) {
//        canvas.drawLine();
        double mean = maxValue / 10;
        for (int i = 0; i < LINES_Y_MAX - 1; i++) {
            String leftText = new DecimalFormat("#.00").format(mean * (1 + i));
            float size = mTextPaint.measureText(leftText + (i + 1));
            canvas.drawText(leftText, -size, viewHeight - itemHeight * (i + 1), mTextPaint);

//            canvas.drawLine(0,viewHeight - itemHeight*(i+1),viewWidth,viewHeight - itemHeight*(i+1),mPaint);
        }
        canvas.drawLine(0, 0, 0, viewHeight, mPaint);
    }


    public void drawAlertLines(Canvas canvas) {

        if (alertLines != 0) {
            float y = (float)(1 - alertLines / maxValue) * viewHeight;
            canvas.drawLine(0, y,viewWidth,y,alertPaint);
            String alertText = " 标准值 : "+new DecimalFormat("#.00").format(alertLines);
            float alertSize = alertPaint.measureText(alertText);
            canvas.drawText(alertText,0, y - textSize,alertPaint);
        }
    }

    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
        chart.setMaxValue(maxValue);
        invalidate();
    }


    public void setAlertLines(double alertLines) {
        this.alertLines = alertLines;
    }

    public void setTitle(String title) {
        this.title = title;
        postInvalidate();
    }

    public void clear(){
        maxValue = 1000;
        chart.clear();
    }

}
